package object.obj2D.bullet;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Graphics;

import com.game.R;

public class Missile extends IBullet{
	static private GL10 gl = Graphics.GetGraphics();
	
	static private int m_texID;
	
	static private float m_width = 57.0f/171;
	static private float m_height = 1f;
	
	static private TexSquare m_bulletSquare = new TexSquare(m_width, m_height);
	static private Texture m_texture;
	
	
	private static final float TRANS_X = 0.8f;
	private static final float TRANS_Y = 0f;
	private static final float TRANS_Z = 0;
	
//	private float m_transX = TRANS_X;
//	private float m_transY = TRANS_Y;
//	private float m_transZ = TRANS_Z;
//	
//	private float m_dTransX = TRANS_X/10;
//	private float m_dTransY = TRANS_Y/10;
//	private float m_dTransZ = 1;
	
	private boolean isDisplay = false;

	private float m_fRotAngle;
	
	public Missile()
	{
		gl = Graphics.GetGraphics();
		m_texture = new Texture();
		m_texID = m_texture.initTexture(R.drawable.rocket);
	}
	
	static void Init(){
		m_bulletSquare = new TexSquare(m_width, m_height);
		m_texture = new Texture();
		m_texID = m_texture.initTexture(R.drawable.rocket);
	}
	
	public Missile(float alpha, float beta, float distance, int damage)
	{
		m_alpha = alpha;
		m_beta = beta;
		m_transX = TRANS_X;
		m_transY = TRANS_Y;
		m_transZ = TRANS_Z;
		
		m_dTransX = TRANS_X/10;
		m_dTransY = TRANS_Y/10;
		m_dTransZ = 1;
		
		SetDistance(distance);
	}
	
	public void SetAlpha(float alpha)
	{
		m_alpha = alpha;
	}
	
	public void SetBeta(float beta)
	{
		m_beta = beta;
	}
	
	public void SetDistance(float distance)
	{		
		m_distance = distance;
		m_dTransX = TRANS_X / distance;
		m_dTransY = TRANS_Y / distance;
		m_dTransZ = 1;
		
		m_fRotAngle = (float)(Math.atan(m_distance/TRANS_X) * 180/ Math.PI);
	}
	
	public float GetAlpha()
	{
		return m_alpha;
	}
	
	public float GetBeta()
	{
		return m_beta;
	}
	
	public float GetDistance()
	{
		return m_beta;
	}
	
	public void Update()
	{
		if(isDisplay)
		{
			m_transX -= m_dTransX;
			m_transY -= m_dTransY;
			m_transZ -= m_dTransZ;
			
			if(m_transZ <= -m_distance)
			{
				m_transX = TRANS_X;
				m_transY = TRANS_Y;
				m_transZ = TRANS_Z;
				
				isDisplay = false;
			}
			
		}
	}
	
	public void Start()
	{
		isDisplay = true;
	}
	
	public boolean isPlaying()
	{
		return isDisplay;
	}
	
	public void Stop()
	{
		m_transX = TRANS_X;
		m_transY = TRANS_Y;
		m_transZ = TRANS_Z;
		
		isDisplay = false;
	}
	
	public void Render()
	{
		if(isDisplay)
		{
			gl.glMatrixMode(GL10.GL_MODELVIEW);			
			gl.glPushMatrix();
			gl.glRotatef(-180 + m_beta, 0, 1.0f, 0);
			gl.glRotatef(90 - m_alpha, 1.0f, 0, 0);
			gl.glTranslatef(m_transX, m_transY, m_transZ);
			
			gl.glRotatef(-m_fRotAngle, 0, 1.0f, 0);
			gl.glRotatef(90, 0, 0, 1.0f);
			m_texture.loadTexture(m_texID);
			
			m_bulletSquare.Render();
			gl.glRotatef(-90, 0, 1.0f, 0);
			m_bulletSquare.Render();
			
			gl.glPopMatrix();
		}
	}
}
